{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2024 Teclib' and contributors.
 # @copyright 2003-2014 by the INDEPNET Development Team.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{% import 'components/form/fields_macros.html.twig' as fields %}

{% if display_actortypes is not defined %}
   {% set display_actortypes = ['requester', 'observer', 'assign'] %}
{% endif %}
{% if display_labels is not defined %}
   {% set display_labels = true %}
{% endif %}

{% set actor_options = field_options|merge({
    is_horizontal: false,
    add_field_class: (is_expanded ? 'col-sm-4' : ''),
}) %}
{% set can_admin = item.canAdminActors() %}
{% if item.isNewItem() %}
   {% set can_admin = true %}
{% endif %}

{# Flag to allow individual fields to auto-submit the form. Only used by the requestor field currently. Some uses of the actor dropdowns outside of a ticket need this to be blocked. #}
{% set allow_auto_submit = allow_auto_submit ?? true %}
{# TODO can_admin is usefull for "assign to me" shortcut #}

{# TODO check allow_email params #}

{% if 'requester' in display_actortypes and (not itiltemplate.isHiddenField('_users_id_requester') or not itiltemplate.isHiddenField('_groups_id_requester')) %}
   {% set rand = random() %}
   {% set requester_field %}
      {{ include('components/itilobject/actors/field.html.twig', {
         'rand': rand,
         'item': item,
         'actortypeint': constant('CommonITILActor::REQUESTER'),
         'actortype': 'requester',
         'entities_id': entities_id,
         'itiltemplate': itiltemplate,
         'params': params,
         'canupdate': canupdate,
         'cancreateuser': cancreateuser,
         'canassigntome': canassigntome ?? false,
         'main_rand': main_rand,
         'allow_auto_submit': allow_auto_submit,
      }, with_context = false) }}
   {% endset %}
   {% if display_labels %}
      {{ fields.field(
         'requester',
         requester_field,
         _n('Requester', 'Requesters', 1),
         actor_options|merge({
            required: itiltemplate.isMandatoryField('_users_id_requester') or itiltemplate.isMandatoryField('_groups_id_requester'),
            id: "actor_" ~ rand,
         })
      ) }}
   {% else %}
      {{ requester_field }}
   {% endif %}
{% endif %}

{% if 'observer' in display_actortypes and (not itiltemplate.isHiddenField('_users_id_observer') or not itiltemplate.isHiddenField('_groups_id_observer')) %}
   {% set rand = random() %}
   {% set observer_field %}
      {{ include('components/itilobject/actors/field.html.twig', {
         'rand': rand,
         'item': item,
         'actortypeint': constant('CommonITILActor::OBSERVER'),
         'actortype': 'observer',
         'entities_id': entities_id,
         'itiltemplate': itiltemplate,
         'params': params,
         'canupdate': canupdate,
         'cancreateuser': cancreateuser,
         'canassigntome': canassigntome ?? false,
         'main_rand': main_rand,
         'allow_auto_submit': allow_auto_submit,
      }, with_context = false) }}
   {% endset %}
   {% if display_labels %}
      {{ fields.field(
         'observer',
         observer_field,
         _n('Observer', 'Observers', 1),
         actor_options|merge({
            required: itiltemplate.isMandatoryField('_users_id_observer') or itiltemplate.isMandatoryField('_groups_id_observer'),
            id: "actor_" ~ rand,
         })
      ) }}
   {% else %}
      {{ observer_field }}
   {% endif %}
{% endif %}

{% if 'assign' in display_actortypes and (not itiltemplate.isHiddenField('_users_id_assign') or not itiltemplate.isHiddenField('_groups_id_assign') or not itiltemplate.isHiddenField('_supplier_id_assign')) %}
   {% set assign_field %}
      {% set rand = random() %}
      {{ include('components/itilobject/actors/field.html.twig', {
         'rand': rand,
         'item': item,
         'actortypeint': constant('CommonITILActor::ASSIGN'),
         'actortype': 'assign',
         'users_right': 'own_ticket',
         'entities_id': entities_id,
         'itiltemplate': itiltemplate,
         'params': params,
         'canupdate': canassign,
         'canassigntome': canassigntome ?? false,
         'main_rand': main_rand,
         'allow_auto_submit': allow_auto_submit,
      }, with_context = false) }}
   {% endset %}
   {% if display_labels %}
      {{ fields.field(
         'assign',
         assign_field,
         __('Assigned to'),
         actor_options|merge({
            required: itiltemplate.isMandatoryField('_users_id_assign') or itiltemplate.isMandatoryField('_groups_id_assign') or itiltemplate.isMandatoryField('_supplier_id_assign'),
            id: "actor_" ~ rand,
         })
      ) }}
   {% else %}
      {{ assign_field }}
   {% endif %}
{% endif %}

<input type="hidden" name="_actors" id="_actors" />

{# modal for edit notification setting for a user #}
<div class="modal fade" id="editActorNotifySettings" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
         <h5 class="modal-title">
            <i class="fas fa-envelope"></i>
            &nbsp;{{ __('Edit notification settings') }}
         </h5>
         <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="{{ __('Close') }}"></button>
      </div>
      <div class="modal-body">
         <div class="mb-3">
            <label class="form-label" for="actorname">{{ __('Actor') }}</label>
            <input type="text" class="form-control" id="actorname" name="_notifications_actorname" />
            <input type="hidden" name="_notifications_actortype" value="" />
            <input type="hidden" name="_notifications_actorindex" value="" />
         </div>
         <div class="mb-3">
            <label for="alternative_email" class="form-label">{{ __('Email address') }}</label>
            <div class="form-check form-switch">
               <input class="form-check-input" type="checkbox" id="use_notification" name="_notifications_use_notification" />
               <label class="form-check-label" for="use_notification">{{ __('Email followup') }}</label>
            </div>
            <div id="alert_notification_disable" class='alert alert-info d-flex align-items-center mb-4 d-none' role='alert'>
               <i class='fas fa-info-circle fa-xl'></i>
               <span class='ms-2'>
                  {{ __('Notifications are disabled because:') }}
                  <ul>
                     <li>{{ __('User does not have an email address.') }}</li>
                     <li>{{ __('User has disabled notifications from its preferences.') }}</li>
                     <li>{{ __('Notifications are disabled in this entity.') }}</li>
                  </ul>
                  {{ __('However, you can reactivate the notifications for this ticket.') }}
               </span>
            </div>
            <input type="email" class="form-control" id="alternative_email" name="_notifications_alternative_email" />
         </div>
      </div>
      <div class="modal-footer">
         <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">
            <i class="ti ti-x"></i>
            <span>{{ __('Close') }}<span>
         </button>
         <button type="button" class="btn btn-primary" id="saveActorNotifySettings">
            <i class="far fa-save"></i>
            <span>{{ __('Save') }}</span>
         </button>
      </div>
    </div>
  </div>
</div>

{% set actortypes = {
   'requester': constant('CommonITILActor::REQUESTER'),
   'observer': constant('CommonITILActor::OBSERVER'),
   'assign': constant('CommonITILActor::ASSIGN')
} %}

<script type="text/javascript">
   /* global bootstrap */
   var editActorNotifySettings_modal = null;
   $(function () {
      editActorNotifySettings_modal = new bootstrap.Modal(document.getElementById('editActorNotifySettings'), {});
   });

   var actors = {
      {% for actortype, actortypeint in actortypes %}
         {{ actortype }}: [
            {% for actor in item.getActorsForType(actortypeint, params) %}
            {
               itemtype: "{{ actor['itemtype'] }}",
               items_id: "{{ actor['items_id'] }}",
               use_notification: {{ actor['use_notification'] ? "1" : "0" }},
               alternative_email: "{{ actor['alternative_email'] ?? '' }}",
               default_email: "{{ actor['default_email'] ?? '' }}",
            },
            {% endfor %}
         ],
      {% endfor %}
   };

   // edit email preference when .edit-notify-user button is activated
   function openNotifyModal(event) {
      event.stopPropagation();

      var element = $(event.target).closest('.actor_entry');
      var itemtype  = element.data('itemtype');
      var items_id  = element.data('items-id');
      var actortype = element.data('actortype');
      var text      = element.find('.actor_text').text().trim();

      actorIndex = actors[actortype].findIndex(element =>
         element.itemtype == itemtype &&
         (items_id === 0 ? element.alternative_email == text : element.items_id == items_id)
      );
      var actor = actors[actortype][actorIndex];

      var modal = $('#editActorNotifySettings');
      modal.find("input[name=_notifications_actortype]").val(actortype);
      modal.find("input[name=_notifications_actorindex]").val(actorIndex);
      modal.find("input[name=_notifications_actorname]").removeAttr('readonly').val(text).attr('readonly', 'true');
      modal.find("input[name=_notifications_use_notification]").prop('checked', parseInt(actor.use_notification));
      modal.find("input[name=_notifications_alternative_email]").attr('placeholder', actor.default_email);
      modal.find("input[name=_notifications_alternative_email]").val(actor.alternative_email);
      modal.find("#alert_notification_disable").toggleClass('d-none', parseInt(actor.use_notification) == true);

      editActorNotifySettings_modal.show();
   }

   // save edited actor when clicking on save button
   $(document).on('click', '#saveActorNotifySettings', function(event) {
      saveNotificationSettings();
   });

   // prevent submit on enter key in modal (but save notification settings)
   $(document).on('keyup keypress', '#editActorNotifySettings input', function(event) {
      var keyCode = event.keyCode || event.which;
      if (keyCode === 13) {
         event.preventDefault();
         saveNotificationSettings();
         return false;
      }
   });

   // save settings from modal (get modal data, and serialize them into input hidden)
   function saveNotificationSettings() {
      var modal = $('#editActorNotifySettings');
      var actortype  = modal.find("input[name=_notifications_actortype]").val();
      var actorIndex = modal.find("input[name=_notifications_actorindex]").val();
      var use_notif  = (modal.find("input[name=_notifications_use_notification]").is(":checked") ? 1 : 0);
      var alt_email  = modal.find("input[name=_notifications_alternative_email]").val();

      var actor = actors[actortype][actorIndex];
      actor.use_notification = use_notif;
      actor.alternative_email = alt_email;

      // update notif setting icon for actor
      var fa_class = "far";
      if (use_notif) {
         fa_class = "fas";
      }

      var actor_entry = $('.actor_entry[data-itemtype='+actor.itemtype+'][data-items-id='+actor.items_id+'][data-actortype='+actortype+']')
         .filter(function() {
            if(actor.items_id == 0 && actor.itemtype == "User") {
               return $(this).text().trim() === actor.alternative_email;
            }
            return $(this).text()
         });

      // toggle notify icon
      actor_entry.find('.notify-icon')
         .removeClass('fas far')
         .addClass(fa_class);

      // toggle text for direct email
      if (actor.itemtype == "User" && parseInt(actor.items_id) == 0) {
         actor_entry.find('.actor_text').html(alt_email);
      }


      editActorNotifySettings_modal.hide();
      saveActorsToDom();
   }

   // save actors object to dom
   function saveActorsToDom() {
      $('#_actors').val(JSON.stringify(actors));
   }
   saveActorsToDom();
</script>
